package com.ruoyi.web.controller.app;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AppLoginController {

    @Autowired
    private SysLoginService loginService;
    
    @Autowired
    private TokenService tokenService;

    @Anonymous
    @PostMapping("/app/wxLogin")
    public AjaxResult wxLogin(@RequestBody LoginBody loginBody) {
        return loginService.wxLogin(loginBody);
    }
    
    /**
     * 刷新令牌
     * 
     * @return 结果
     */
    @GetMapping("/app/refreshToken")
    public AjaxResult refreshToken() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser != null) {
            // 刷新令牌有效期
            tokenService.refreshToken(loginUser);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("token", loginUser.getToken());
            return ajax;
        }
        return AjaxResult.error("令牌已失效");
    }
}
